unit Smfe;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Buttons, Grids, ExtCtrls, SockMsgs, DsgnIntf;

type
  TMsgRegFieldsEditor = class(TForm)
    Panel1: TPanel;
    SGFields: TStringGrid;
    BitBtn1: TBitBtn;
    BAdd: TBitBtn;
    BDelete: TBitBtn;
    BUp: TBitBtn;
    BDown: TBitBtn;
    BEdit: TBitBtn;
    Label1: TLabel;
    PSize: TPanel;
    procedure FormShow(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BDeleteClick(Sender: TObject);
    procedure BUpClick(Sender: TObject);
    procedure BDownClick(Sender: TObject);
    procedure BAddClick(Sender: TObject);
    procedure BEditClick(Sender: TObject);
  private
    { Private declarations }
    Fields : TMsgRegFieldDefs;
    procedure ShowData;
  public
    { Public declarations }
    procedure Edit( f: TMsgRegFieldDefs; Designer: TDesigner );
  end;

implementation

{$R *.DFM}

uses
  Smfef;

procedure TMsgRegFieldsEditor.Edit( f: TMsgRegFieldDefs; Designer: TDesigner );
begin
  Caption := 'Fields of '+Designer.Form.Name+'.'+f.Owner.Name;
  Fields := f;
  ShowModal;
end;

procedure TMsgRegFieldsEditor.ShowData;
var
  i : integer;
begin
  with SGFields do
  begin
    if Fields.Count>0 then
    begin
      RowCount := Fields.Count+1;
      BDelete.Enabled := true;
      BEdit.Enabled := true;
      BUp.Enabled := Fields.Count>1;
      BDown.Enabled := Fields.Count>1;
    end
    else
    begin
      RowCount := 2;
      Rows[1].Clear;
      BDelete.Enabled := false;
      BEdit.Enabled := false;
      BUp.Enabled := false;
      BDown.Enabled := false;
    end;
    Cells[0,0] := 'Name';
    Cells[1,0] := 'Size';
    Cells[2,0] := 'Decs';
    for i := 0 to Fields.Count-1 do
    begin
      Cells[0,i+1] := Fields.Items[i].Name;
      Cells[1,i+1] := IntToStr(Fields.Items[i].Size);
      Cells[2,i+1] := IntToStr(Fields.Items[i].Decimals);
    end;
  end;
  PSize.Caption := IntToStr(Fields.Size);
end;

procedure TMsgRegFieldsEditor.FormShow(Sender: TObject);
begin
  ShowData;
end;

procedure TMsgRegFieldsEditor.BitBtn1Click(Sender: TObject);
begin
  Close;
end;

procedure TMsgRegFieldsEditor.BDeleteClick(Sender: TObject);
begin
  Fields.Items[SGFields.Row-1].Free;
  ShowData;
end;

procedure TMsgRegFieldsEditor.BUpClick(Sender: TObject);
begin
  if (Fields.Count=1) or (SGFields.Row-1=0) then exit;
  Fields.Exchange(SGFields.Row-1,SGFields.Row-2);
  ShowData;
  SGFields.Row := SGFields.Row-1;
end;

procedure TMsgRegFieldsEditor.BDownClick(Sender: TObject);
begin
  if (Fields.Count=1) or (SGFields.Row-1=Fields.Count-1) then exit;
  Fields.Exchange(SGFields.Row-1,SGFields.Row);
  ShowData;
  SGFields.Row := SGFields.Row+1;
end;

procedure TMsgRegFieldsEditor.BAddClick(Sender: TObject);
var
  e : TFieldEditor;
begin
  e := TFieldEditor.Create(Self);
  if e.Execute('Add Field') then
  begin
    Fields.Add(e.Name,e.Size,e.Decimals);
    ShowData;
  end;
  e.Free;
end;

procedure TMsgRegFieldsEditor.BEditClick(Sender: TObject);
var
  e : TFieldEditor;
begin
  e := TFieldEditor.Create(Self);
  with Fields.Items[SGFields.Row-1] do
  begin
    e.Name := Name;
    e.Size := Size;
    e.Decimals := Decimals;
    if e.Execute('Edit Field') then
    begin
      Name := e.Name;
      Size := e.Size;
      Decimals := e.Decimals;
      ShowData;
    end;
  end;
  e.Free;
end;

end.
